package cn.dj.order.service.impl;

import cn.dj.basic.util.LoginContext;
import cn.dj.basic.util.PageList;
import cn.dj.order.domain.OrderPetAcquisition;
import cn.dj.order.dto.OrderDto;
import cn.dj.order.mapper.OrderPetAcquisitionMapper;
import cn.dj.order.query.OrderPetAcquisitionQuery;
import cn.dj.order.service.IOrderPetAcquisitionService;
import cn.dj.basic.service.impl.BaseServiceImpl;
import cn.dj.org.domain.Employee;
import cn.dj.org.domain.Shop;
import cn.dj.org.mapper.EmployeeMapper;
import cn.dj.org.mapper.ShopMapper;
import cn.dj.pet.domain.Pet;
import cn.dj.pet.domain.SearchMasterMsg;
import cn.dj.pet.mapper.PetMapper;
import cn.dj.pet.mapper.SearchMasterMsgMapper;
import cn.dj.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

/**
 * 业务实现类：宠物收购订单
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {
    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired//注入
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private PetMapper petMapper;

    @Override
    public PageList<OrderPetAcquisition> queryPage(OrderPetAcquisitionQuery query, HttpServletRequest request) {
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());
        if (employee.getShop_id() != null) {//店铺管理员:有店长or店员 【店长能看店铺所有宠物收购订单】
            Shop shop = shopMapper.loadById(employee.getShop_id());
            if (shop.getAdminId() != employee.getId()) {//店员：只能看到自己处理的宠物收购订单
                query.setEmpId(employee.getId());//动态sql进行分页/高级查询的限制
            }
            query.setShopId(employee.getShop_id());//动态sql进行分页/高级查询的限制
        }
        //平台管理员看所有的订单
        return super.queryPage(query);
    }

    @Override
    @Transactional
    public void confirm(OrderDto orderDto) {
        //订单确认：处理人去用户那里把宠物接回来 - 平台就会多一个宠物，需要录入(还没上架)
        Long orderId = orderDto.getId();
        BigDecimal money = orderDto.getMoney();//最终成交价
        Integer payType = orderDto.getPayType();//支付方式

        //1.生成宠物对象
        OrderPetAcquisition order = orderPetAcquisitionMapper.loadById(orderId);
        SearchMasterMsg msg = searchMasterMsgMapper.loadById(order.getSearchMasterMsgId());
        Pet pet = createPet(msg, money);
        petMapper.save(pet);
        //2.修改订单的状态为1，并且要修改金额，最终成交价【成交价=成本价】
        order.setState(1);//订单完成，接回宠物，成交了
        order.setPrice(money);
        order.setPetId(pet.getId());
        orderPetAcquisitionMapper.update(order);
        //3.修改寻主消息的状态：state=2
        msg.setState(2);//寻主消息处理完成
        searchMasterMsgMapper.update(msg);
    }

    private Pet createPet(SearchMasterMsg msg, BigDecimal money) {
        Pet pet = new Pet();
        pet.setCostprice(money);
        pet.setName(msg.getName());
        pet.setSaleprice(money);
        pet.setTypeId(msg.getPetType());
        pet.setResources(msg.getResources());
        pet.setShopId(msg.getShopId());
//        pet.setUserId(msg.getUserId()); //这个是领养人的id，才收回宠物，不应该还没处理之后上架就被领养了
        pet.setSearchMasterMsgId(msg.getId());
        return pet;
    }

    @Override
    @Transactional
    public void cancel(Long id) {//传的就是当前行这个订单的id
        //1.将寻主消息放入寻主池：state=3，shopId=null
        OrderPetAcquisition order = orderPetAcquisitionMapper.loadById(id);
            //修改寻主消息的状态state=3，放入寻主池，并且把先前分配的店铺id置为null
        searchMasterMsgMapper.reject(order.getSearchMasterMsgId());//寻主消息的shop_id置为null【订单中不用，那是历史订单】

        //2.修改订单的状态为-1【取消了】
        order.setState(-1);
        orderPetAcquisitionMapper.update(order);//更新状态 ， 作为历史订单遗留在那。里面的信息都是历史信息了
    }
}
